package org.gwu.seuss.server.command;

import org.gwu.seuss.communication.domain.UserTO;
import org.gwu.seuss.communication.request.Actions;
import org.gwu.seuss.communication.request.ClientRequest;
import org.gwu.seuss.communication.request.ServerResponse;
import org.springframework.util.Assert;

public class EnableUserCommand extends UserCommandBase implements Command {

	public EnableUserCommand(ClientRequest req) {
		super(req);
	}
	
	@Override
	public ServerResponse execute() throws Exception {
		String username = req.getUsername();
		String password = req.getPassword();
		UserTO enableUser = req.getUser();
		
		Assert.hasText(username, "Username is not provided.");
		Assert.hasText(password, "Password is not provided.");
		Assert.notNull(enableUser, "User to enable is not provided");

		userService.actionAllowed(username, Actions.ENABLE_USER);
		log.info("User " + username + " requested to enable user " + enableUser.getUsername());
		
		int result = userService.enableUser(enableUser.getUsername());
		
		return getServerResponse(result, "User " + enableUser.getUsername() + " enabled.");
	}
}